home *** CD-ROM | disk | FTP | other *** search
/ Mac Mania 2 / MacMania 2.toast / Demo's / Tools&Utilities / Programming / SPIM Folder / Sources / inst.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-04  |  32.5 KB  |  1,425 lines  |  [TEXT/ttxt]

  1. /* SPIM S20 MIPS simulator.
  2.    Code to build assembly instructions and resolve symbolic labels.
  3.    Copyright (C) 1990 by James Larus (larus@cs.wisc.edu).
  4.  
  5.    SPIM is free software; you can redistribute it and/or modify it
  6.    under the terms of the GNU General Public License as published by the
  7.    Free Software Foundation; either version 1, or (at your option) any
  8.    later version.
  9.  
  10.    SPIM is distributed in the hope that it will be useful, but WITHOUT
  11.    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12.    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13.    for more details.
  14.  
  15.    You should have received a copy of the GNU General Public License
  16.    along with GNU CC; see the file COPYING.  If not, write to James R.
  17.    Larus, Computer Sciences Department, University of Wisconsin--Madison,
  18.    1210 West Dayton Street, Madison, WI 53706, USA or to the Free
  19.    Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
  20.  
  21.  
  22. /* $Header: /var/home/cs354/.spim/RCS/inst.c,v 1.4 1992/10/12 11:33:58 cs354 Exp $ */
  23.  
  24. #include <stdio.h>
  25. #include "spim.h"
  26. #include "inst.h"
  27. #include "mem.h"
  28. #include "reg.h"
  29. #include "sym_tbl.h"
  30. #ifdef WIN32
  31. #include "y_tab.h"
  32. #else
  33. #ifdef MACINTOSH
  34. #include "y_tab.h"
  35. #else
  36. #include "y.tab.h"
  37. #endif
  38. #endif
  39. #include "data.h"
  40.  
  41. /* Internal functions: */
  42.  
  43. typedef int (*CompareFunc)();
  44.  
  45. static    instruction *mk_inst(int opcode,int rs,int rt,int rd,int shamt,int offset,int target);
  46. static    void inst_cmp(instruction *inst1, instruction *inst2);
  47. static    int compare_pair_value(const inst_info *p1,const inst_info *p2);
  48. static    void sort_name_table(void);
  49. static    void sort_i_opcode_table(void);
  50. static    void sort_a_opcode_table(void);
  51. static    int print_imm_expr(char *buf,const imm_expr *expr,int base_reg);
  52. static  void i_type_inst_full_word(int opcode,int rt,int rs,
  53.     imm_expr *expr, int value_known,long value);
  54.  
  55. /* Imported functions: */
  56.  
  57. imm_expr *branch_offset (int);
  58. int imm_op_to_op (int);
  59.  
  60.  
  61. /* Imported variables: */
  62.  
  63. extern int data_dir;
  64. extern int text_dir;
  65.  
  66.  
  67. /* Non-zero means store instructions in kernel, not user, text segment */
  68.  
  69. int in_kernel = 0;
  70.  
  71.  
  72. /* Locations for next instruction in user and kernel text segments */
  73.  
  74. static mem_addr next_text_pc;
  75.  
  76. static mem_addr next_k_text_pc;
  77.  
  78. unsigned int last_inst_addr;
  79.  
  80. #define INST_PC (in_kernel ? next_k_text_pc : next_text_pc)
  81.  
  82.  
  83. #define BUMP_INST_PC(DELTA) {if (in_kernel) \
  84.                    next_k_text_pc += DELTA; \
  85.                    else next_text_pc += DELTA;}
  86.  
  87.  
  88.  
  89. /* Set ADDRESS at which the next instruction is stored. */
  90.  
  91. void text_begins_at_point (mem_addr addr)
  92. {
  93.   next_text_pc = addr;
  94. }
  95.  
  96.  
  97. void k_text_begins_at_point (mem_addr addr)
  98. {
  99.   next_k_text_pc = addr;
  100. }
  101.  
  102.  
  103. /* Return address for next instruction, in appropriate text segment. */
  104.  
  105. mem_addr current_text_pc (void)
  106. {
  107.   return (INST_PC);
  108. }
  109.  
  110.  
  111. /* Increment the current text segement PC. */
  112.  
  113. void increment_text_pc (int delta)
  114. {
  115.   BUMP_INST_PC (delta);
  116. }
  117.  
  118.  
  119. /* If FLAG is non-zero, next instruction goes to kernel text segment,
  120.    otherwise it goes to user segment. */
  121.  
  122. void
  123. user_kernel_text_segment (to_kernel)
  124.      int to_kernel;
  125. {
  126.   in_kernel = to_kernel;
  127. }
  128.  
  129.  
  130. /* Store an INSTRUCTION in memory at the next location. */
  131.  
  132. void store_instruction (instruction *inst)
  133. {
  134.   if (data_dir)
  135.     {
  136.       store_word (inst_encode (inst));
  137.       free (inst);
  138.     }
  139.   else if (text_dir)
  140.     {
  141.       exception_occurred = 0;
  142.       last_inst_addr = INST_PC;
  143.       SET_MEM_INST (INST_PC, inst);
  144.       if (exception_occurred) {
  145.     sprintf(mess_buff, "Invalid address (0x%08x) for instruction\n",
  146.           INST_PC);
  147.     error (mess_buff);
  148.       }
  149.       else
  150.     BUMP_INST_PC (BYTES_PER_WORD);
  151.     }
  152. }
  153.  
  154.  
  155.  
  156. /* Produce an immediate instruction with the OPCODE, RT, RS, and IMM
  157.    fields.  NB, because the immediate value may not fit in the field,
  158.    this routine may produce more than one instruction.    On the bare
  159.    machine, we resolve symbolic address, but they better produce values
  160.    that fit into instruction's immediate field. */
  161.  
  162. void i_type_inst (int opcode, int rt, int rs, imm_expr *expr)
  163. {
  164.   instruction *inst = (instruction *) malloc (sizeof (instruction));
  165.  
  166.   if ( !inst )
  167.     fatal_error("Ran out of memory in i_type_inst().\n");
  168.  
  169.   bzero (inst, sizeof (instruction));
  170.   OPCODE (inst) = opcode;
  171.   RS (inst) = rs;
  172.   RT (inst) = rt;
  173.   EXPR (inst) = expr;
  174.   if (expr->symbol == NULL || SYMBOL_IS_DEFINED (expr->symbol))
  175.     {
  176.       /* Evaluate the instruction's expression. */
  177.       long value = eval_imm_expr (expr);
  178.  
  179.       if (!bare_machine
  180.       && (value & 0xffff0000) != 0
  181.       && !((value & 0xffff0000) == 0xffff0000
  182.            && (opcode == Y_ADDI_OP
  183.            || opcode == Y_ADDIU_OP
  184.            || opcode == Y_SLTI_OP
  185.            || opcode == Y_SLTIU_OP)))
  186.     {
  187.       free (inst);
  188.       i_type_inst_full_word (opcode, rt, rs, expr, 1, value);
  189.       return;
  190.     }
  191.       else
  192.     resolve_a_label (expr->symbol, inst, INST_PC);
  193.     }
  194.   else if (bare_machine || expr->bits != 0)
  195.     /* Don't know expressions value, but only needed upper/lower 16-bits
  196.        anyways. */
  197.     record_inst_uses_symbol (inst, INST_PC, expr->symbol);
  198.   else
  199.     {
  200.       /* Don't know the expressions's value and want all of its bits,
  201.      so assume that it will not produce a small result and generate
  202.      sequence for 32 bit value. */
  203.       free (inst);
  204.  
  205.       i_type_inst_full_word (opcode, rt, rs, expr, 0, 0);
  206.       return;
  207.     }
  208.  
  209.   store_instruction (inst);
  210. }
  211.  
  212.  
  213. /* The immediate value for an instruction will (or may) not fit in 16 bits.
  214.    Build the value from its piece with separate instructions. */
  215.  
  216. static void i_type_inst_full_word (int opcode, int rt, int rs, 
  217.     imm_expr *expr, int value_known, long value)
  218. {
  219.   if (opcode_is_load_store (opcode))
  220.     {
  221.       long offset;
  222.  
  223.       if (expr->symbol != NULL
  224.       && expr->symbol->gp_flag && rs == 0
  225.       && (offset = expr->symbol->addr + expr->offset) >= -32*K
  226.       && offset < 32*K)
  227.     i_type_inst (opcode, rt, REG_GP, make_imm_expr (offset, NULL, 0));
  228.       else if (value_known)
  229.     {
  230.       int low, high;
  231.  
  232.       high = (value >> 16) & 0xffff;
  233.       low = 0xffff & value;
  234.  
  235.       if (high &&
  236.           !(high == 0xffff && (low & 0x8000))) /* load sign-extends */
  237.         {
  238.           if (low & 0x8000)
  239.         high += 1;        /* Adjust since load sign-extends */
  240.  
  241.           i_type_inst (Y_LUI_OP, 1, 0, const_imm_expr (high));
  242.           if (rs != 0)    /* Base register */
  243.         r_type_inst (Y_ADDU_OP, 1, 1, rs);
  244.           i_type_inst (opcode, rt, 1, const_imm_expr (low));
  245.         }
  246.       else
  247.         i_type_inst (opcode, rt, rs, const_imm_expr (low));
  248.     }
  249.       else
  250.     {
  251.       /* Use $at */
  252.       /* Need to adjust if lower bits are negative */
  253.       i_type_inst (Y_LUI_OP, 1, 0, upper_bits_of_expr (expr));
  254.       if (rs != 0)        /* Base register */
  255.         r_type_inst (Y_ADDU_OP, 1, 1, rs);
  256.       i_type_inst (opcode, rt, 1, lower_bits_of_expr (expr));
  257.     }
  258.     }
  259.   else if (opcode_is_branch (opcode))
  260.     {
  261.       /* This only allows branches +/- 32K, which is not correct! */
  262.       i_type_inst (opcode, rt, rs, lower_bits_of_expr (expr));
  263.     }
  264.   else
  265.     /* Computation instruction */
  266.     {
  267.       long offset;
  268.  
  269.       if (expr->symbol != NULL
  270.       && expr->symbol->gp_flag && rs == 0
  271.       && (offset = expr->symbol->addr + expr->offset) >= -32*K
  272.       && offset < 32*K)
  273.     i_type_inst ((opcode == Y_LUI_OP ? Y_ADDIU_OP : opcode),
  274.              rt, REG_GP, make_imm_expr (offset, NULL, 0));
  275.       else
  276.     {
  277.       /* Use $at */
  278.       if ((opcode == Y_ORI_OP
  279.            || opcode == Y_ADDI_OP || opcode == Y_ADDIU_OP
  280.            || opcode == Y_LUI_OP)
  281.           && rs == 0)
  282.         {
  283.           if (value_known && (value & 0xffff) == 0)
  284.         i_type_inst (Y_LUI_OP, rt, 0, upper_bits_of_expr (expr));
  285.           else
  286.         {
  287.           i_type_inst (Y_LUI_OP, 1, 0, upper_bits_of_expr (expr));
  288.           i_type_inst (Y_ORI_OP, rt, 1, lower_bits_of_expr (expr));
  289.         }
  290.         }
  291.       else
  292.         {
  293.           i_type_inst (Y_LUI_OP, 1, 0, upper_bits_of_expr (expr));
  294.           i_type_inst (Y_ORI_OP, 1, 1, lower_bits_of_expr (expr));
  295.           r_type_inst (imm_op_to_op (opcode), rt, rs, 1);
  296.         }
  297.     }
  298.     }
  299. }
  300.  
  301.  
  302. /* Return a jump-type instruction with the given OPCODE and TARGET
  303.    fields. NB, even the immediate value may not fit in the field, this
  304.    routine will not produce more than one instruction. */
  305.  
  306. void j_type_inst (int opcode, imm_expr *target)
  307. {
  308.   instruction *inst = (instruction *) malloc (sizeof (instruction));
  309.  
  310.   if ( !inst )
  311.     fatal_error("Ran out of memory in j_type_inst().\n");
  312.  
  313.   bzero (inst, sizeof (instruction));
  314.   OPCODE(inst) = opcode;
  315.   target->offset = 0;        /* Not PC relative */
  316.   target->pc_relative = 0;
  317.   EXPR (inst) = target;
  318.   if (target->symbol == NULL || SYMBOL_IS_DEFINED (target->symbol))
  319.     resolve_a_label (target->symbol, inst, INST_PC);
  320.   else
  321.     record_inst_uses_symbol (inst, INST_PC, target->symbol);
  322.   store_instruction (inst);
  323. }
  324.  
  325.  
  326. /* Return a register-type instruction with the given OPCODE, RD, RS, and RT
  327.    fields. */
  328.  
  329. instruction *r_type_inst (int opcode, int rd, int rs, int rt)
  330. {
  331.   instruction *inst = (instruction *) malloc (sizeof (instruction));
  332.  
  333.   if ( !inst )
  334.     fatal_error("Ran out of memory in r_type_inst().\n");
  335.  
  336.   bzero (inst, sizeof (instruction));
  337.   OPCODE(inst) = opcode;
  338.   RS(inst) = rs;
  339.   RT(inst) = rt;
  340.   RD(inst) = rd;
  341.   SHAMT(inst) = 0;
  342.   store_instruction (inst);
  343.   return (inst);
  344. }
  345.  
  346.  
  347. /* Return a register-shift instruction with the given OPCODE, RD, RT, and
  348.    SHAMT fields.*/
  349.  
  350. void r_sh_type_inst (int opcode, int rd, int rt, int shamt)
  351. {
  352.   instruction *inst = r_type_inst (opcode, rd, 0, rt);
  353.  
  354.   SHAMT(inst) = shamt & 0x1f;
  355. }
  356.  
  357.  
  358. /* Return a floating-point compare instruction with the given OPCODE,
  359.    FS, and FT fields.*/
  360.  
  361. void r_cond_type_inst (int opcode, int rs, int rt)
  362. {
  363.   instruction *inst = r_type_inst (opcode, 0, rs, rt);
  364.  
  365.   switch (opcode)
  366.     {
  367.     case Y_C_EQ_D_OP:
  368.     case Y_C_EQ_S_OP:
  369.       {
  370.     COND(inst) = COND_EQ;
  371.     break;
  372.       }
  373.  
  374.     case Y_C_LE_D_OP:
  375.     case Y_C_LE_S_OP:
  376.       {
  377.     COND(inst) = COND_IN | COND_LT | COND_EQ;
  378.     break;
  379.       }
  380.  
  381.     case Y_C_LT_D_OP:
  382.     case Y_C_LT_S_OP:
  383.       {
  384.     COND(inst) = COND_IN | COND_LT;
  385.     break;
  386.       }
  387.  
  388.     case Y_C_NGE_D_OP:
  389.     case Y_C_NGE_S_OP:
  390.       {
  391.     COND(inst) = COND_IN | COND_LT | COND_UN;
  392.     break;
  393.       }
  394.  
  395.     case Y_C_NGLE_D_OP:
  396.     case Y_C_NGLE_S_OP:
  397.       {
  398.     COND(inst) = COND_IN | COND_UN;
  399.     break;
  400.       }
  401.  
  402.     case Y_C_NGL_D_OP:
  403.     case Y_C_NGL_S_OP:
  404.       {
  405.     COND(inst) = COND_IN | COND_EQ | COND_UN;
  406.     break;
  407.       }
  408.  
  409.     case Y_C_NGT_D_OP:
  410.     case Y_C_NGT_S_OP:
  411.       {
  412.     COND(inst) = COND_IN | COND_LT | COND_EQ | COND_UN;
  413.     break;
  414.       }
  415.  
  416.     case Y_C_OLE_D_OP:
  417.     case Y_C_OLE_S_OP:
  418.       {
  419.     COND(inst) = COND_LT | COND_EQ;
  420.     break;
  421.       }
  422.  
  423.     case Y_C_SEQ_D_OP:
  424.     case Y_C_SEQ_S_OP:
  425.       {
  426.     COND(inst) = COND_IN | COND_EQ;
  427.     break;
  428.       }
  429.  
  430.     case Y_C_SF_D_OP:
  431.     case Y_C_SF_S_OP:
  432.       {
  433.     COND(inst) = COND_IN;
  434.     break;
  435.       }
  436.  
  437.     case Y_C_F_D_OP:
  438.     case Y_C_F_S_OP:
  439.       {
  440.     COND(inst) = 0;
  441.     break;
  442.       }
  443.  
  444.     case Y_C_UEQ_D_OP:
  445.     case Y_C_UEQ_S_OP:
  446.       {
  447.     COND(inst) = COND_EQ | COND_UN;
  448.     break;
  449.       }
  450.  
  451.     case Y_C_ULE_D_OP:
  452.     case Y_C_ULE_S_OP:
  453.       {
  454.     COND(inst) = COND_LT | COND_EQ | COND_UN;
  455.     break;
  456.       }
  457.  
  458.     case Y_C_UN_D_OP:
  459.     case Y_C_UN_S_OP:
  460.       {
  461.     COND(inst) = COND_UN;
  462.     break;
  463.       }
  464.     }
  465. }
  466.  
  467.  
  468.  
  469. /* Maintain a table mapping from opcode to instruction name and
  470.    instruction type.
  471.  
  472.    Table must be sorted before first use since its entries are
  473.    alphabetical on name, not ordered by opcode. */
  474.  
  475. static int sorted_name_table = 0;    /* Non-zero => table sorted */
  476.  
  477.  
  478. /* Map from opcode -> name/type. */
  479.  
  480. static inst_info name_tbl [] = {
  481. #include "y_tab.h"
  482. #undef OP
  483. #define OP(NAME, OPCODE, TYPE, R_OPCODE) {NAME, OPCODE, TYPE},
  484. #include "op.h"
  485. };
  486.  
  487.  
  488. /* Compare the VALUE1 field of two INST_INFO entries in the format
  489.    required by qsort. */
  490.  
  491. static int compare_pair_value (const inst_info *p1, const inst_info *p2)
  492. {
  493.   if (p1->value1 < p2->value1)
  494.     return (-1);
  495.   else if (p1->value1 > p2->value1)
  496.     return (1);
  497.   else
  498.     return (0);
  499. }
  500.  
  501.  
  502. /* Sort the opcode table on their key (the opcode value). */
  503.  
  504. static void sort_name_table (void)
  505. {
  506.   void qsort ();
  507.  
  508.   qsort (name_tbl,
  509.      sizeof (name_tbl) / sizeof (inst_info),
  510.      sizeof (inst_info),
  511.      (CompareFunc) compare_pair_value);
  512.   sorted_name_table = 1;
  513. }
  514.  
  515.  
  516. /* Print the instruction stored at the memory ADDRESS. */
  517.  
  518. void print_inst (mem_addr addr)
  519. {
  520.   instruction *inst;
  521.   char buf [128];
  522.  
  523.   exception_occurred = 0;
  524.   READ_MEM_INST (inst, addr);
  525.  
  526.   if (exception_occurred)
  527.     {
  528.       sprintf(mess_buff,
  529.         "Can't print instruction not in text segment (0x%08x)\n", addr);
  530.       error (mess_buff);
  531.       return;
  532.     }
  533.   print_inst_internal (buf, inst, addr);
  534.   write_output (message_out, buf);
  535. }
  536.  
  537.  
  538. int print_inst_internal (char *buf, instruction *inst, mem_addr addr)
  539. {
  540.   char *bp = buf;
  541.   inst_info *entry;
  542.  
  543.   if (!sorted_name_table)
  544.     sort_name_table ();
  545.  
  546.   sprintf (buf, "[0x%08x]\t", addr);
  547.   buf += strlen (buf);
  548.   if (inst == NULL)
  549.     {
  550.       sprintf (buf, "<none>\n");
  551.       buf += strlen (buf);
  552.       return (buf - bp);
  553.     }
  554.   entry = map_int_to_inst_info (name_tbl,
  555.                 sizeof (name_tbl) / sizeof (inst_info),
  556.                 OPCODE (inst));
  557.   if (entry == NULL)
  558.     {
  559.       sprintf (buf, "<unknown instruction %d>\n", OPCODE (inst));
  560.       buf += strlen (buf);
  561.       return (buf - bp);
  562.     }
  563.   sprintf (buf, "0x%08x  %s", inst_encode (inst), entry->name);
  564.   buf += strlen (buf);
  565.   switch (entry->value2)
  566.     {
  567.     case B0_TYPE_INST:
  568.       sprintf (buf, " %d", SIGN_EX (IMM (inst) << 2));
  569.       buf += strlen (buf);
  570.       break;
  571.  
  572.     case B1_TYPE_INST:
  573.       sprintf (buf, " $%d %d", RS (inst), SIGN_EX (IMM (inst) << 2));
  574.       buf += strlen (buf);
  575.       break;
  576.  
  577.     case I1t_TYPE_INST:
  578.       sprintf (buf, " $%d, %d", RT (inst), IMM (inst));
  579.       buf += strlen (buf);
  580.       break;
  581.  
  582.     case I2_TYPE_INST:
  583.       sprintf (buf, " $%d, $%d, %d", RT (inst), RS (inst), IMM (inst));
  584.       buf += strlen (buf);
  585.       break;
  586.  
  587.     case B2_TYPE_INST:
  588.       sprintf (buf, " $%d, $%d, %d", RS (inst), RT (inst),
  589.            SIGN_EX (IMM (inst) << 2));
  590.       buf += strlen (buf);
  591.       break;
  592.  
  593.     case I2a_TYPE_INST:
  594.       sprintf (buf, " $%d, %d($%d)", RT (inst), IMM (inst), BASE (inst));
  595.       buf += strlen (buf);
  596.       break;
  597.  
  598.     case R1s_TYPE_INST:
  599.       sprintf (buf, " $%d", RS (inst));
  600.       buf += strlen (buf);
  601.       break;
  602.  
  603.     case R1d_TYPE_INST:
  604.       sprintf (buf, " $%d", RD (inst));
  605.       buf += strlen (buf);
  606.       break;
  607.  
  608.     case R2td_TYPE_INST:
  609.       sprintf (buf, " $%d, $%d", RT (inst), RD (inst));
  610.       buf += strlen (buf);
  611.       break;
  612.  
  613.     case R2st_TYPE_INST:
  614.       sprintf (buf, " $%d, $%d", RS (inst), RT (inst));
  615.       buf += strlen (buf);
  616.       break;
  617.  
  618.     case R2ds_TYPE_INST:
  619.       sprintf (buf, " $%d, $%d", RD (inst), RS (inst));
  620.       buf += strlen (buf);
  621.       break;
  622.  
  623.     case R2sh_TYPE_INST:
  624.       sprintf (buf, " $%d, $%d, %d", RD (inst), RT (inst), SHAMT (inst));
  625.       buf += strlen (buf);
  626.       break;
  627.  
  628.     case R3_TYPE_INST:
  629.       sprintf (buf, " $%d, $%d, $%d", RD (inst), RS (inst), RT (inst));
  630.       buf += strlen (buf);
  631.       break;
  632.  
  633.     case R3sh_TYPE_INST:
  634.       sprintf (buf, " $%d, $%d, $%d", RD (inst), RT (inst), RS (inst));
  635.       buf += strlen (buf);
  636.       break;
  637.  
  638.     case FP_I2a_TYPE_INST:
  639.       sprintf (buf, " $f%d, %d($%d)", FT (inst), IMM (inst), BASE (inst));
  640.       buf += strlen (buf);
  641.       break;
  642.  
  643.     case FP_R2ds_TYPE_INST:
  644.       sprintf (buf, " $f%d, $f%d", FD (inst), FS (inst));
  645.       buf += strlen (buf);
  646.       break;
  647.  
  648.     case FP_R2st_TYPE_INST:
  649.       sprintf (buf, " $f%d, $f%d", FS (inst), FT (inst));
  650.       buf += strlen (buf);
  651.       break;
  652.  
  653.     case FP_R3_TYPE_INST:
  654.       sprintf (buf, " $f%d, $f%d, $f%d", FD (inst), FS (inst), FT (inst));
  655.       buf += strlen (buf);
  656.       break;
  657.  
  658.     case FP_MOV_TYPE_INST:
  659.       sprintf (buf, " $f%d, $f%d", FD (inst), FS (inst));
  660.       buf += strlen (buf);
  661.       break;
  662.  
  663.     case J_TYPE_INST:
  664.       sprintf (buf, " 0x%08x", TARGET (inst) << 2);
  665.       buf += strlen (buf);
  666.       break;
  667.  
  668.     case CP_TYPE_INST:
  669.       sprintf (buf, " $%d, $%d", RT (inst), RD (inst));
  670.       buf += strlen (buf);
  671.       break;
  672.  
  673.     case NOARG_TYPE_INST:
  674.       break;
  675.  
  676.     case ASM_DIR:
  677.     case PSEUDO_OP:
  678.     default:
  679.       fatal_error ("Unknown instruction type in print_inst\n");
  680.     }
  681.  
  682.   /*
  683.   if (EXPR (inst) != NULL)
  684.     {
  685.       sprintf (buf, " [");
  686.       buf += strlen (buf);
  687.       if (opcode_is_load_store (OPCODE (inst)))
  688.     buf += print_imm_expr (buf, EXPR (inst), BASE (inst));
  689.       else
  690.     buf += print_imm_expr (buf, EXPR (inst), -1);
  691.       sprintf (buf, "]");
  692.       buf += strlen (buf);
  693.     }
  694.   */
  695.  
  696.   if (COMMENT(inst) != NULL)
  697.     {
  698.       sprintf(buf, "\t# %s", COMMENT(inst));
  699.       buf += strlen(buf);
  700.     }
  701.  
  702.   sprintf (buf, "\n");
  703.   buf += strlen (buf);
  704.   return (buf - bp);
  705. }
  706.  
  707.  
  708.  
  709. /* Return non-zero if an INSTRUCTION is a conditional branch. */
  710.  
  711. int opcode_is_branch (int opcode)
  712. {
  713.   switch (opcode)
  714.     {
  715.     case Y_BEQ_OP:
  716.     case Y_BEQZ_POP:
  717.     case Y_BGE_POP:
  718.     case Y_BGEU_POP:
  719.     case Y_BGEZ_OP:
  720.     case Y_BGEZAL_OP:
  721.     case Y_BGT_POP:
  722.     case Y_BGTU_POP:
  723.     case Y_BGTZ_OP:
  724.     case Y_BLE_POP:
  725.     case Y_BLEU_POP:
  726.     case Y_BLEZ_OP:
  727.     case Y_BLT_POP:
  728.     case Y_BLTU_POP:
  729.     case Y_BLTZ_OP:
  730.     case Y_BLTZAL_OP:
  731.     case Y_BNE_OP:
  732.     case Y_BNEZ_POP:
  733.     case Y_BC1F_OP:
  734.     case Y_BC1T_OP:
  735.       return (1);
  736.  
  737.     default:
  738.       return (0);
  739.     }
  740. }
  741.  
  742.  
  743. /* Return non-zero if an INSTRUCTION is an conditional branch (jump). */
  744.  
  745. int opcode_is_jump (int opcode)
  746. {
  747.   switch (opcode)
  748.     {
  749.     case Y_J_OP:
  750.     case Y_JAL_OP:
  751.       return (1);
  752.  
  753.     default:
  754.       return (0);
  755.     }
  756. }
  757.  
  758. /* Return non-zero if an INSTRUCTION is a load or store. */
  759.  
  760. int opcode_is_load_store (int opcode)
  761. {
  762.   switch (opcode)
  763.     {
  764.     case Y_LB_OP: return (1);
  765.     case Y_LBU_OP: return (1);
  766.     case Y_LH_OP: return (1);
  767.     case Y_LHU_OP: return (1);
  768.     case Y_LW_OP: return (1);
  769.     case Y_LWC0_OP: return (1);
  770.     case Y_LWC1_OP: return (1);
  771.     case Y_LWC2_OP: return (1);
  772.     case Y_LWC3_OP: return (1);
  773.     case Y_LWL_OP: return (1);
  774.     case Y_LWR_OP: return (1);
  775.     case Y_SB_OP: return (1);
  776.     case Y_SH_OP: return (1);
  777.     case Y_SW_OP: return (1);
  778.     case Y_SWC0_OP: return (1);
  779.     case Y_SWC1_OP: return (1);
  780.     case Y_SWC2_OP: return (1);
  781.     case Y_SWC3_OP: return (1);
  782.     case Y_SWL_OP: return (1);
  783.     case Y_SWR_OP: return (1);
  784.     case Y_L_D_POP: return (1);
  785.     case Y_L_S_POP: return (1);
  786.     case Y_S_D_POP: return (1);
  787.     case Y_S_S_POP: return (1);
  788.     default: return (0);
  789.     }
  790. }
  791.  
  792.  
  793. static instruction *break_inst = NULL;
  794.  
  795. #ifdef MACINTOSH
  796. /* When we re-init the world, we must nullify all malloc'ed pointers, because the
  797.    malloc memory supply gets completely erased. This includes pointers that are
  798.    hard to find, such as break_inst;
  799. */
  800.  
  801. void ReinitInst()
  802. {
  803.     break_inst = NULL;
  804. }
  805.  
  806. #endif
  807.  
  808.  
  809. /* Return non-zero if a breakpoint is set at ADDR. */
  810.  
  811. int inst_is_breakpoint (mem_addr addr)
  812. {
  813.   instruction *old_inst;
  814.  
  815.   if (break_inst == NULL)
  816.     break_inst = r_type_inst (Y_BREAK_OP, 1, 0, 0);
  817.  
  818.   READ_MEM_INST (old_inst, addr);
  819.   return (old_inst == break_inst);
  820. }
  821.  
  822.  
  823. /* Set a breakpoint at ADDR and return the old instruction.  If the
  824.    breakpoint cannot be set, return NULL. */
  825.  
  826. instruction * set_breakpoint (mem_addr addr)
  827. {
  828.   instruction *old_inst;
  829.  
  830.   if (break_inst == NULL)
  831.     break_inst = r_type_inst (Y_BREAK_OP, 1, 0, 0);
  832.  
  833.   exception_occurred = 0;
  834.   READ_MEM_INST (old_inst, addr);
  835.   if (old_inst == break_inst)
  836.     return (NULL);
  837.   SET_MEM_INST (addr, break_inst);
  838.   if (exception_occurred)
  839.     return (NULL);
  840.   else
  841.     return (old_inst);
  842. }
  843.  
  844.  
  845.  
  846. /* An immediate expression has the form: SYMBOL +/- IOFFSET, where either
  847.    part may be omitted. */
  848.  
  849. /* Make and return a new immediate expression */
  850.  
  851. imm_expr *make_imm_expr (int offs, char *sym, int pc_rel)
  852. {
  853.   imm_expr *expr = (imm_expr *) malloc (sizeof (imm_expr));
  854.  
  855.   if ( !expr )
  856.     fatal_error("Ran out of memory in make_imm_expr().\n");
  857.  
  858.   expr->offset = offs;
  859.   expr->bits = 0;
  860.   expr->pc_relative = pc_rel;
  861.   if (sym != NULL)
  862.     expr->symbol = lookup_label (sym);
  863.   else
  864.     expr->symbol = NULL;
  865.   return (expr);
  866. }
  867.  
  868.  
  869. /* Return a shallow copy of the EXPRESSION. */
  870.  
  871. imm_expr * copy_imm_expr (imm_expr *old_expr)
  872. {
  873.   imm_expr *expr = (imm_expr *) malloc (sizeof (imm_expr));
  874.  
  875.   if ( !expr )
  876.     fatal_error("Ran out of memory in copy_imm_expr().\n");
  877.  
  878.   bcopy (old_expr, expr, sizeof (imm_expr));
  879.   return (expr);
  880. }
  881.  
  882.  
  883. /* Return a shallow copy of an EXPRESSION that only uses the upper
  884.    sixteen bits of the expression's value. */
  885.  
  886. imm_expr * upper_bits_of_expr (imm_expr *old_expr)
  887. {
  888.   imm_expr *expr = copy_imm_expr (old_expr);
  889.  
  890.   expr->bits = 1;
  891.   return (expr);
  892. }
  893.  
  894.  
  895. /* Return a shallow copy of the EXPRESSION that only uses the lower
  896.    sixteen bits of the expression's value. */
  897.  
  898. imm_expr *lower_bits_of_expr (imm_expr *old_expr)
  899. {
  900.   imm_expr *expr = copy_imm_expr (old_expr);
  901.  
  902.   expr->bits = -1;
  903.   return (expr);
  904. }
  905.  
  906.  
  907. /* Return an instruction expression for a constant VALUE. */
  908.  
  909. imm_expr *const_imm_expr (long value)
  910. {
  911.   return (make_imm_expr (value, NULL, 0));
  912. }
  913.  
  914.  
  915. /* Return a shallow copy of the EXPRESSION with the offset field
  916.    incremented by the given amount. */
  917.  
  918. imm_expr * incr_expr_offset (imm_expr *expr, long value)
  919. {
  920.   imm_expr *new_expr = copy_imm_expr (expr);
  921.  
  922.   new_expr->offset += value;
  923.   return (new_expr);
  924. }
  925.  
  926.  
  927. /* Return the value of the EXPRESSION. */
  928.  
  929. long eval_imm_expr (imm_expr *expr)
  930. {
  931.   long value;
  932.  
  933.   if (expr->symbol == NULL)
  934.     value = expr->offset;
  935.   else if (SYMBOL_IS_DEFINED (expr->symbol))
  936.     {
  937.       value = expr->offset + expr->symbol->addr;
  938.       if (expr->symbol->gp_flag) /* Addr is offset from $gp */
  939.     value += gp_midpoint;
  940.     }
  941.   else
  942.     {
  943.       sprintf(mess_buff, "Evaluated undefined symbol: %s\n",
  944.         expr->symbol->name);
  945.       error (mess_buff);
  946.       value = 0;
  947.     }
  948.   if (expr->bits > 0)
  949.     return ((value >> 16) & 0xffff); /* Use upper bits of result */
  950.   else if (expr->bits < 0)
  951.     return (value & 0xffff);    /* Use lower bits */
  952.   else
  953.     return (value);
  954. }
  955.  
  956.  
  957. /* Print the EXPRESSION. */
  958.  
  959. static int print_imm_expr (char *buf, const imm_expr *expr, int base_reg)
  960. {
  961.   char *bp = buf;
  962.  
  963.   if (expr->symbol != NULL)
  964.     sprintf (buf, "%s", expr->symbol->name);
  965.   buf += strlen(buf);
  966.  
  967.   if (expr->pc_relative)
  968.     sprintf (buf, "-0x%08x", -expr->offset);
  969.   else if (expr->offset < -10)
  970.     sprintf (buf, "-%d (-0x%08x)", -expr->offset, -expr->offset);
  971.   else if (expr->offset > 10)
  972.     sprintf (buf, "+%d (0x%08x)", expr->offset, expr->offset);
  973.  
  974.   buf += strlen(buf);
  975.  
  976.   if (base_reg != -1 && expr->symbol != NULL &&
  977.       (expr->offset > 10 || expr->offset < -10))
  978.     {
  979.       if (expr->offset == 0 && base_reg != 0)
  980.     sprintf (buf, "+0");
  981.       if (expr->offset != 0 || base_reg != 0)
  982.     sprintf (buf, "($%d)", base_reg);
  983.     }
  984.   buf += strlen(buf);
  985.  
  986.   return (buf - bp);
  987. }
  988.  
  989.  
  990. /* Return non-zero if the EXPRESSION is a constant 0. */
  991.  
  992. int zero_imm (imm_expr *expr)
  993. {
  994.   return (expr->offset == 0 && expr->symbol == NULL);
  995. }
  996.  
  997.  
  998.  
  999. /* Return an address expression of the form SYMBOL +/- IOFFSET (REGISTER).
  1000.    Any of the three parts may be omitted. */
  1001.  
  1002. addr_expr * make_addr_expr (long offs, char *sym, int reg_no)
  1003. {
  1004.   addr_expr *expr = (addr_expr *) malloc (sizeof (addr_expr));
  1005.   label *lab;
  1006.  
  1007.   if ( !expr )
  1008.     fatal_error("Ran out of memory in make_addr_expr().\n");
  1009.  
  1010.   if (reg_no == 0 && sym != NULL && (lab = lookup_label (sym))->gp_flag)
  1011.     {
  1012.       expr->reg_no = REG_GP;
  1013.       expr->imm = make_imm_expr (offs + lab->addr - gp_midpoint, NULL, 0);
  1014.     }
  1015.   else
  1016.     {
  1017.       expr->reg_no = reg_no;
  1018.       expr->imm = make_imm_expr (offs, sym, 0);
  1019.     }
  1020.   return (expr);
  1021. }
  1022.  
  1023.  
  1024. imm_expr * addr_expr_imm (addr_expr *expr)
  1025. {
  1026.   return (expr->imm);
  1027. }
  1028.  
  1029.  
  1030. int addr_expr_reg (addr_expr *expr)
  1031. {
  1032.   return (expr->reg_no);
  1033. }
  1034.  
  1035.  
  1036.  
  1037. /* Map between a SPIM instruction and the binary representation of the
  1038.    instruction. */
  1039.  
  1040.  
  1041. /* Maintain a table mapping from internal opcode (i_opcode) to actual
  1042.    opcode (a_opcode).  Table must be sorted before first use since its
  1043.    entries are alphabetical on name, not ordered by opcode. */
  1044.  
  1045. static int sorted_i_opcode_table = 0; /* Non-zero => table sorted */
  1046.  
  1047.  
  1048. /* Map from internal opcode -> real opcode */
  1049.  
  1050. static inst_info i_opcode_tbl [] = {
  1051. #undef OP
  1052. #define OP(NAME, I_OPCODE, TYPE, A_OPCODE) {NAME, I_OPCODE, A_OPCODE},
  1053. #include "op.h"
  1054. };
  1055.  
  1056.  
  1057. /* Sort the opcode table on their key (the interal opcode value). */
  1058.  
  1059. static void sort_i_opcode_table (void)
  1060. {
  1061.   void qsort ();
  1062.  
  1063.   qsort (i_opcode_tbl,
  1064.      sizeof (i_opcode_tbl) / sizeof (inst_info),
  1065.      sizeof (inst_info),
  1066.      (CompareFunc) compare_pair_value);
  1067.   sorted_i_opcode_table = 1;
  1068. }
  1069.  
  1070.  
  1071. #define REGS(R,O) (((R) & 0x1f) << O)
  1072.  
  1073.  
  1074. long inst_encode (instruction *inst)
  1075. {
  1076.   long a_opcode = 0;
  1077.   inst_info *entry;
  1078.  
  1079.   if (inst == NULL)
  1080.     return (0);
  1081.   if (!sorted_i_opcode_table)
  1082.     sort_i_opcode_table ();
  1083.   if (!sorted_name_table)
  1084.     sort_name_table ();
  1085.  
  1086.   entry = map_int_to_inst_info (i_opcode_tbl,
  1087.                 sizeof (i_opcode_tbl) / sizeof (inst_info),
  1088.                 OPCODE (inst));
  1089.   if (entry == NULL)
  1090.     return 0;
  1091.  
  1092.   a_opcode = entry->value2;
  1093.   entry = map_int_to_inst_info (name_tbl,
  1094.                 sizeof (name_tbl) / sizeof (inst_info),
  1095.                 OPCODE (inst));
  1096.  
  1097.   switch (entry->value2)
  1098.     {
  1099.     case B0_TYPE_INST:
  1100.       return (a_opcode
  1101.           | IOFFSET (inst) & 0xffff);
  1102.  
  1103.     case B1_TYPE_INST:
  1104.       return (a_opcode
  1105.           | REGS (RS (inst), 21)
  1106.           | IOFFSET (inst) & 0xffff);
  1107.  
  1108.     case I1t_TYPE_INST:
  1109.       return (a_opcode
  1110.           | REGS (RS (inst), 21)
  1111.           | REGS (RT (inst), 16)
  1112.           | IMM (inst) & 0xffff);
  1113.  
  1114.     case I2_TYPE_INST:
  1115.     case B2_TYPE_INST:
  1116.       return (a_opcode
  1117.           | REGS (RS (inst), 21)
  1118.           | REGS (RT (inst), 16)
  1119.           | IMM (inst) & 0xffff);
  1120.  
  1121.     case I2a_TYPE_INST:
  1122.       return (a_opcode
  1123.           | REGS (BASE (inst), 21)
  1124.           | REGS (RT (inst), 16)
  1125.           | IOFFSET (inst) & 0xffff);
  1126.  
  1127.     case R1s_TYPE_INST:
  1128.       return (a_opcode
  1129.           | REGS (RS (inst), 21));
  1130.  
  1131.     case R1d_TYPE_INST:
  1132.       return (a_opcode
  1133.           | REGS (RD (inst), 11));
  1134.  
  1135.     case R2td_TYPE_INST:
  1136.       return (a_opcode
  1137.           | REGS (RT (inst), 16)
  1138.           | REGS (RD (inst), 11));
  1139.  
  1140.     case R2st_TYPE_INST:
  1141.       return (a_opcode
  1142.           | REGS (RS (inst), 21)
  1143.           | REGS (RT (inst), 16));
  1144.  
  1145.     case R2ds_TYPE_INST:
  1146.       return (a_opcode
  1147.           | REGS (RS (inst), 21)
  1148.           | REGS (RD (inst), 11));
  1149.  
  1150.     case R2sh_TYPE_INST:
  1151.       return (a_opcode
  1152.           | REGS (RT (inst), 16)
  1153.           | REGS (RD (inst), 11)
  1154.           | REGS (SHAMT (inst), 6));
  1155.  
  1156.     case R3_TYPE_INST:
  1157.       return (a_opcode
  1158.           | REGS (RS (inst), 21)
  1159.           | REGS (RT (inst), 16)
  1160.           | REGS (RD (inst), 11));
  1161.  
  1162.     case R3sh_TYPE_INST:
  1163.       return (a_opcode
  1164.           | REGS (RS (inst), 21)
  1165.           | REGS (RT (inst), 16)
  1166.           | REGS (RD (inst), 11));
  1167.  
  1168.     case FP_I2a_TYPE_INST:
  1169.       return (a_opcode
  1170.           | REGS (BASE (inst), 21)
  1171.           | REGS (RT (inst), 16)
  1172.           | (IOFFSET (inst) & 0xffff));
  1173.  
  1174.     case FP_R2ds_TYPE_INST:
  1175.       return (a_opcode
  1176.           | REGS (FS (inst), 11)
  1177.           | REGS (FD (inst), 6));
  1178.  
  1179.     case FP_R2st_TYPE_INST:
  1180.       return (a_opcode
  1181.           | REGS (FT (inst), 16)
  1182.           | REGS (FS (inst), 11));
  1183.  
  1184.     case FP_R3_TYPE_INST:
  1185.       return (a_opcode
  1186.           | REGS (FT (inst), 16)
  1187.           | REGS (FS (inst), 11)
  1188.           | REGS (FD (inst), 6));
  1189.  
  1190.     case FP_MOV_TYPE_INST:
  1191.       return (a_opcode
  1192.           | REGS (FS (inst), 11)
  1193.           | REGS (FD (inst), 6));
  1194.  
  1195.     case J_TYPE_INST:
  1196.       return (a_opcode
  1197.           | TARGET (inst));
  1198.  
  1199.     case CP_TYPE_INST:
  1200.       return (a_opcode
  1201.           | REGS (RT (inst), 16)
  1202.           | REGS (RD (inst), 11));
  1203.  
  1204.     case NOARG_TYPE_INST:
  1205.       return (a_opcode);
  1206.  
  1207.     case ASM_DIR:
  1208.     case PSEUDO_OP:
  1209.     default:
  1210.       fatal_error ("Unknown instruction type in inst_encoding\n");
  1211.       return (0);        /* Not reached */
  1212.     }
  1213. }
  1214.  
  1215.  
  1216. /* Maintain a table mapping from actual opcode to interal opcode.
  1217.    Table must be sorted before first use since its entries are
  1218.    alphabetical on name, not ordered by opcode. */
  1219.  
  1220. static int sorted_a_opcode_table = 0; /* Non-zero => table sorted */
  1221.  
  1222.  
  1223. /* Map from internal opcode -> real opcode */
  1224.  
  1225. static inst_info a_opcode_tbl [] = {
  1226. #undef OP
  1227. #define OP(NAME, I_OPCODE, TYPE, A_OPCODE) {NAME, A_OPCODE, I_OPCODE},
  1228. #include "op.h"
  1229. };
  1230.  
  1231.  
  1232. /* Sort the opcode table on their key (the interal opcode value). */
  1233.  
  1234. static void sort_a_opcode_table (void)
  1235. {
  1236.   void qsort ();
  1237.  
  1238.   qsort (a_opcode_tbl,
  1239.      sizeof (a_opcode_tbl) / sizeof (inst_info),
  1240.      sizeof (inst_info),
  1241.      (CompareFunc) compare_pair_value);
  1242.   sorted_a_opcode_table = 1;
  1243. }
  1244.  
  1245.  
  1246. #define REG(V,O) ((V) >> O) & 0x1f
  1247.  
  1248.  
  1249. instruction * inst_decode (long value)
  1250. {
  1251.   long a_opcode = value & 0xfc000000;
  1252.   inst_info *entry;
  1253.   long i_opcode;
  1254.  
  1255.   if (a_opcode == 0)        /* SPECIAL */
  1256.     a_opcode |= (value & 0x3f);
  1257.   else if (a_opcode == 0x04000000) /* BCOND */
  1258.     a_opcode |= (value & 0x001f0000);
  1259.   else if (a_opcode == 0x40000000) /* COP0 */
  1260.     a_opcode |= (value & 0x03e00000) | (value & 0x1f);
  1261.   else if (a_opcode == 0x44000000) /* COP1 */
  1262.     {
  1263.       a_opcode |= (value & 0x03e00000);
  1264.       if ((value & 0xff000000) == 0x45000000)
  1265.     a_opcode |= (value & 0x00010000); /* BC1f/t */
  1266.       else
  1267.     a_opcode |= (value & 0x3f);
  1268.     }
  1269.   else if (a_opcode == 0x48000000 /* COPz */
  1270.        || a_opcode == 0x4c000000)
  1271.     a_opcode |= (value & 0x03e00000);
  1272.  
  1273.  
  1274.   if (!sorted_a_opcode_table)
  1275.     sort_a_opcode_table ();
  1276.   if (!sorted_name_table)
  1277.     sort_name_table ();
  1278.  
  1279.   entry = map_int_to_inst_info (a_opcode_tbl,
  1280.                 sizeof (a_opcode_tbl) / sizeof (inst_info),
  1281.                 a_opcode);
  1282.   if (entry == NULL)
  1283.     return (NULL);
  1284.   i_opcode = entry->value2;
  1285.  
  1286.   switch (map_int_to_inst_info (name_tbl,
  1287.                 sizeof (name_tbl) / sizeof (inst_info),
  1288.                 i_opcode)->value2)
  1289.     {
  1290.     case B0_TYPE_INST:
  1291.       return (mk_inst (i_opcode, 0, 0, 0, 0, value & 0xffff, 0));
  1292.  
  1293.     case B1_TYPE_INST:
  1294.       return (mk_inst (i_opcode, REG (value, 21), 0, 0, 0,
  1295.                value & 0xffff, 0));
  1296.  
  1297.     case I1t_TYPE_INST:
  1298.       return (mk_inst (i_opcode, REG (value, 21), REG (value, 16), 0, 0,
  1299.                value & 0xffff, 0));
  1300.  
  1301.     case I2_TYPE_INST:
  1302.     case B2_TYPE_INST:
  1303.       return (mk_inst (i_opcode, REG (value, 21), REG (value, 16), 0, 0,
  1304.                value & 0xffff, 0));
  1305.  
  1306.     case I2a_TYPE_INST:
  1307.       return (mk_inst (i_opcode, REG (value, 21), REG (value, 16), 0, 0,
  1308.                value & 0xffff, 0));
  1309.  
  1310.     case R1s_TYPE_INST:
  1311.       return (mk_inst (i_opcode, REG (value, 21), 0, 0, 0, 0, 0));
  1312.  
  1313.     case R1d_TYPE_INST:
  1314.       return (mk_inst (i_opcode, 0, 0, REG (value, 11), 0, 0, 0));
  1315.  
  1316.     case R2td_TYPE_INST:
  1317.       return (mk_inst (i_opcode, 0, REG (value, 16), REG (value, 11), 0,
  1318.                0, 0));
  1319.  
  1320.     case R2st_TYPE_INST:
  1321.       return (mk_inst (i_opcode, REG (value, 21), REG (value, 16), 0, 0,
  1322.                0, 0));
  1323.  
  1324.     case R2ds_TYPE_INST:
  1325.       return (mk_inst (i_opcode, REG (value, 21), 0, REG (value, 11), 0,
  1326.                0, 0));
  1327.  
  1328.     case R2sh_TYPE_INST:
  1329.       return (mk_inst (i_opcode, 0, REG (value, 16), REG (value, 11),
  1330.                REG (value, 6), 0, 0));
  1331.  
  1332.     case R3_TYPE_INST:
  1333.       return (mk_inst (i_opcode, REG (value, 21), REG (value, 16),
  1334.                REG (value, 11), 0, 0, 0));
  1335.  
  1336.     case R3sh_TYPE_INST:
  1337.       return (mk_inst (i_opcode, REG (value, 21), REG (value, 16),
  1338.                REG (value, 11), 0, 0, 0));
  1339.  
  1340.     case FP_I2a_TYPE_INST:
  1341.       return (mk_inst (i_opcode, REG (value, 21), REG (value, 16), 0, 0,
  1342.                value & 0xffff, 0));
  1343.  
  1344.     case FP_R2ds_TYPE_INST:
  1345.       return (mk_inst (i_opcode, REG (value, 11), 0, REG (value, 6), 0,
  1346.                0, 0));
  1347.  
  1348.     case FP_R2st_TYPE_INST:
  1349.       return (mk_inst (i_opcode, REG (value, 11), REG (value, 16), 0, 0,
  1350.                value & 0xf, 0));
  1351.  
  1352.     case FP_R3_TYPE_INST:
  1353.       return (mk_inst (i_opcode, REG (value, 11), REG (value, 16),
  1354.                REG (value, 6), 0, 0, 0));
  1355.  
  1356.     case FP_MOV_TYPE_INST:
  1357.       return (mk_inst (i_opcode, REG (value, 11), 0, REG (value, 6), 0,
  1358.                0, 0));
  1359.  
  1360.     case J_TYPE_INST:
  1361.       return (mk_inst (i_opcode, 0, 0, 0, 0, 0, value & 0x2ffffff));
  1362.  
  1363.     case CP_TYPE_INST:
  1364.       return (mk_inst (i_opcode, 0, REG (value, 16), REG (value, 11), 0,
  1365.                0, 0));
  1366.  
  1367.     case NOARG_TYPE_INST:
  1368.       return (mk_inst (i_opcode, 0, 0, 0, 0, 0, 0));
  1369.  
  1370.     case ASM_DIR:
  1371.     case PSEUDO_OP:
  1372.     default:
  1373.       fatal_error ("Unknown instruction type in inst_encoding\n");
  1374.       return (NULL);        /* Not reached */
  1375.     }
  1376. }
  1377.  
  1378.  
  1379. static instruction *mk_inst (int opcode, int rs, int rt, int rd, int shamt, 
  1380.     int offset, int target)
  1381. {
  1382.   instruction *inst = (instruction *) malloc (sizeof (instruction));
  1383.  
  1384.   if ( !inst )
  1385.     fatal_error("Ran out of memory in mk_inst().\n");
  1386.  
  1387.   bzero (inst, sizeof (inst));
  1388.   OPCODE (inst) = opcode;
  1389.   RS (inst) = rs;
  1390.   RT (inst) = rt;
  1391.   RD (inst) = rd;
  1392.   SHAMT (inst) = shamt;
  1393.   IOFFSET (inst) = offset;
  1394.   TARGET (inst) = target;
  1395.   EXPR (inst) = NULL;
  1396.   return (inst);
  1397. }
  1398.  
  1399.  
  1400. /* Code to test encode/decode of instructions. */
  1401.  
  1402. void test_assembly (instruction *inst)
  1403. {
  1404.   instruction *new_inst = inst_decode (inst_encode (inst));
  1405.  
  1406.   inst_cmp (inst, new_inst);
  1407.   free (new_inst);
  1408. }
  1409.  
  1410.  
  1411. static void inst_cmp (instruction *inst1, instruction *inst2)
  1412. {
  1413.   char buf[1024];
  1414.  
  1415.   if (bcmp (inst1, inst2, sizeof (instruction) - 4))
  1416.     {
  1417.       printf ("=================== Not Equal ===================\n");
  1418.       print_inst_internal (buf, inst1, 0);
  1419.       printf ("%s\n", buf);
  1420.       print_inst_internal (buf,inst2, 0);
  1421.       printf ("%s\n", buf);
  1422.       printf ("=================== Not Equal ===================\n");
  1423.     }
  1424. }
  1425.